# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don"t forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


import redis
import demjson
import scrapy
import random
import requests as rq
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline

class MoviesPipeline(object):
    def process_item(self, item, spider):
        if spider.name == 'movie_url':
            all = dict(item)
            sel = dict()
            sel["name"] = all["name"]
            sel["second_name"] = all["second_name"]
            sel["url"] = all["url"]
            sel["starts"] = all["starts"]
            r = redis.Redis(password='5tgbnhy67ujm', db="3")
            r.lpush('qqlive:start_url', all["url"])
            r.lpush('qqlive:movies_all', demjson.encode(sel, encoding='utf-8'))
        elif spider.name == 'comment':
            it = dict(item)
            need = dict()
            user = dict()
            try:
                need["userhead"] = it["userhead"]
                need["usernick"] = it["usernick"]
                user["userhead"] = need["userhead"]
                user["usernick"] = need["usernick"]
            except:
                pass
            need["ctime"] = it["ctime"]
            need["getup"] = it["getup"]
            need["content"] = it["content"]
            need["userid"] = it["userid"]
            r = redis.Redis(password='5tgbnhy67ujm', db="3")
            r.lpush("qqlive:comment", demjson.encode(need, encoding='utf-8'))
            r.lpush('qqlive:users', demjson.encode(user, encoding='utf-8'))
        else:
            pass
        return item


class ImgPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        if 'userhead' in item.keys():
            yield scrapy.Request(item['userhead'], meta=item)

    def file_path(self, request, response=None, info=None):
        item = request.meta
        if 'usernick' in item.keys():
            dic = random.randint(1, 10)
            img_name = './%s/' % dic + item['usernick'].strip('') + '.jpg'
        else:
            img_name = None
        return img_name


class GetipPipeline(object):
    def __init__(self):
        self.r = redis.Redis(password='5tgbnhy67ujm', db='3')

    def process_item(self, item, spider):
        if spider.name == 'xici' or spider.name == 'kuai':
            ips = dict(item)
            if ips['protocol'] == 'http':
                check = {'http': '%s://%s:%s' % (ips['protocol'], ips['host'], ips['port'])}
            elif ips['protocol'] == 'https':
                check = {'https': '%s://%s:%s' % (ips['protocol'], ips['host'], ips['port'])}
            try:
                if rq.get('http://www.baidu.com', proxies=check, timeout=2).status_code == 200:
                    self.r.lpush('ip:%s' % ips['protocol'], check[ips['protocol']])
                return item
            except:
                raise DropItem('%s is error' % check)
        else:
            pass

class QqcomentPipeline(object):
    def process_item(self, item, spider):
        return item
